package guoanmaker.operator.business.controller;

import com.aliyun.oss.OSSClient;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorRoundPhoto;
import guoanmaker.operator.business.model.OperatorRoundPhotoApp;
import guoanmaker.operator.business.service.OperatorRoundPhotoAppServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import guoanmaker.operator.helper.common.util.OSSUnitHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 *  轮播图
 *
 * @author huZhaoyu 
 * @ClassName: OperatorRoundPhotoAppController
 * @Description: OperatorRoundPhotoAppController 
 * @date 2017/6/28 上午10:09 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_ROUNDPHOTO_APP_URL)
public class OperatorRoundPhotoAppController {
    private final OperatorRoundPhotoAppServiceInterface OperatorRoundPhotoAppServiceInterface;

    public OperatorRoundPhotoAppController( OperatorRoundPhotoAppServiceInterface OperatorRoundPhotoAppServiceInterface) {
        this.OperatorRoundPhotoAppServiceInterface = OperatorRoundPhotoAppServiceInterface;
    }

    /** 
    * @Title: saveRoundPhoto 
    * @Description: 新增/修改APP轮播图 
    * @param: OperatorRoundPhotoApp   
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "保存APP轮播图", notes = "新增/修改APP轮播图")
    @PostMapping(value = "saveRoundPhoto")
    public AjaxJson saveRoundPhoto(@RequestBody InRoundPhone inRoundPhone) {
    	AjaxJson ajaxJson=new AjaxJson();
    	System.out.println("www");
    	
    	OperatorRoundPhotoApp[] pps=inRoundPhone.getOperatorRoundPhotoapps();
    	try{
    		OperatorRoundPhotoAppServiceInterface.delectAllBycitycode(inRoundPhone.getCityId());
    	for(OperatorRoundPhotoApp pp:pps){
    		pp.setCreateDate(new Date());
    		pp.setCityId(inRoundPhone.getCityId());
    		OperatorRoundPhotoAppServiceInterface.saveRoundPhoto(pp);
    	}
    	ajaxJson.setSuccess(true).setStatus(200);
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250);
    	}
        return ajaxJson;
    }

    /** 
     * @Title: deleteRoundPhotoById
     * @Description: 根据Id删除APP轮播图
     * @param: String id  
     * @return AjaxJson 
     * @throws 
     */
    @ApiOperation(value = "删除APP轮播图", notes = "根据Id删除APP轮播图")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteRoundPhotoById(@PathVariable(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        OperatorRoundPhotoAppServiceInterface.deleteRoundPhotoById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
    }

    /**
     * @return OperatorRoundPhotoApp    
     * @Title: getRoundPhotoById 
     * @Description: 根据Id查询APP轮播图
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询APP轮播图", notes = "根据Id查询APP轮播图")
    @GetMapping(value = "getRoundPhotoById")
    public AjaxJson getRoundPhotoById(@RequestParam(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(OperatorRoundPhotoAppServiceInterface.getRoundPhotoById(id));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllRoundPhoto
     * @Description: 查找所有APP轮播图 
     * @throws 
     */
    @ApiOperation(value = "查找所有APP轮播图", notes = "查找所有APP轮播图")
    @PostMapping(value = "getAllRoundPhoto")
    public AjaxJson getAllRoundPhoto(String  cityId) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try{
    	List<OperatorRoundPhotoApp>  rpspps=OperatorRoundPhotoAppServiceInterface.getAllRoundphoto(cityId);
    	ajaxJson.setSuccess(true).setStatus(200).setData(rpspps);
    	}catch(Exception e){
    		ajaxJson.setSuccess(true).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

    /**
     * @return AjaxJson   
     * @Title: getAllRoundPhotoByPage 
     * @Description: 查询所有APP轮播图并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询APP轮播图", notes = "分页查询APP轮播图")
    @GetMapping(value = "getAllRoundPhotoByPage")
    public AjaxJson getAllRoundPhotoByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	return ajaxJson.setSuccess(true).setStatus(200).setData(OperatorRoundPhotoAppServiceInterface.getAllRoundPhotoByPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllRoundPhotoByWeightAndPage 
     * @Description: 按权重排序分页查询APP轮播图 
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询APP轮播图", notes = "分页查询APP轮播图")
    @GetMapping(value = "getAllRoundPhotoByWeightAndPage")
    public AjaxJson getAllRoundPhotoByWeightAndPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	return ajaxJson.setSuccess(true).setStatus(200).setData(OperatorRoundPhotoAppServiceInterface.getAllRoundPhotoByWeightAndPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: uploadPhoto 
     * @Description: 上传轮播图片
     * @param: MultipartFile file 图片
     * @throws 
     */
    @ApiOperation(value = "上传轮播图片", notes = "上传轮播图片")
    @PostMapping(value = "uploadPhoto")
//    @ApiImplicitParam(paramType = "form", name = "portraitFile", dataType = "File")
    public AjaxJson upload(@RequestParam(required = false) MultipartFile file){
//        OSSUnitHelper ossunit = new OSSUnitHelper();
    	AjaxJson ajaxJson=new AjaxJson();
    	OSSClient client = OSSUnitHelper.getOSSClient();
        String diskName = "operator/commodity/";
        
        String fileName = new Date().getTime() + (int) ((Math.random() * 9 + 1) * 100000)
				+ file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
		
        
        String resultStr = OSSUnitHelper.uploadObject2OSS(client, file, diskName,fileName);//上传图片到oss
        String url = OSSUnitHelper.getUrl(client, diskName, fileName);//获取图片在oss上的路径
        return ajaxJson.setSuccess(true).setStatus(200).setData(url);
    }

}
